﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Messaging;
using WorkingFileManager;
using WorkingFileManager.MVVM;

namespace Sample.ViewModel
{
    class MainWindowViewModel : ViewModelBase, IWorkingFileOwner
    {
        private WorkingFileState _workingFileState;

        public MainWindowViewModel()
        {

            _workingFileState = new WorkingFileState();
            _workingFileState.FileStateOrWorkingFileChangedAction = () =>
            {
                NotifyPropertyChanged("WindowTitle");
            };

            DefaultFileCommandImplementations = new DefaultFileCommandImplementations(this);
        }

        public DefaultFileCommandImplementations DefaultFileCommandImplementations { get; private set; }

        public WorkingFileState GetWorkingFileState()
        {
            return _workingFileState;
        }

        /// <summary>
        /// The content of the text editor window.
        /// </summary>
        public string Text
        {
            get
            {
                return _Text;
            }
            set
            {
                if (_Text != value)
                {
                    _Text = value;
                    _workingFileState.ReportPerformedFileEvent(WorkingFileState.PerformedFileEvents.Dirtied, null);
                    NotifyPropertyChanged("Text");
                }
            }
        }
        private string _Text;

        /// <summary>
        /// Gets the string that will be bound to the window title.
        /// </summary>
        public string WindowTitle
        {
            get
            {
                switch (_workingFileState.FileState)
                {
                    case WorkingFileState.FileStates.NoWorkingFile_Dirty:
                        return "WorkingFileManager Sample - Untitled*";
                    case WorkingFileState.FileStates.NoWorkingFile_Clean:
                        return "WorkingFileManager Sample - Untitled";
                    case WorkingFileState.FileStates.WorkingFile_Dirty:
                        return "WorkingFileManager Sample - " + _workingFileState.WorkingFileName + "*";
                    case WorkingFileState.FileStates.WorkingFile_Clean:
                        return "WorkingFileManager Sample - " + _workingFileState.WorkingFileName;
                    default:
                        throw new Exception();
                }
            }
        }

        public void CoreNewFile()
        {
            Text = "";
            _workingFileState.ReportPerformedFileEvent(WorkingFileState.PerformedFileEvents.New, null);
        }

        public void CoreSaveFile(string fileName)
        {
            File.WriteAllText(fileName, Text);
            _workingFileState.ReportPerformedFileEvent(WorkingFileState.PerformedFileEvents.Save, fileName);
        }

        public void CoreOpenFile(string fileName)
        {
            try
            {
                Text = File.ReadAllText(fileName);
                _workingFileState.ReportPerformedFileEvent(WorkingFileState.PerformedFileEvents.Open, fileName);
            }
            catch (FileNotFoundException)
            {
                //todo send message to view
                _workingFileState.ReportPerformedFileEvent(WorkingFileState.PerformedFileEvents.OpenFailed, fileName);
            }
        }

        public string GetInitialDirectory(FileActionTypes fileActionType) { return null; }
        public string GetFilter(FileActionTypes fileActionType) { return "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; }
        public string GetWindowText(FileActionTypes fileActionType) { return "Save changes first?"; }
        public string GetWindowTitle(FileActionTypes fileActionType) { return "Save changes?"; }
    }
}
